package com.demo.service.impl;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.demo.dto.EasyUIGrid;
import com.demo.dto.EasyUIPage;
import com.demo.entity.Role;
import com.demo.entity.User;
import com.demo.mapper.UserMapper;
import com.demo.pojo.UserQuery;
import com.demo.service.UserService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

@Service
@Transactional
public class UserServiceImpl extends AbstractBaseService<User> implements UserService {

	@Resource
	private UserMapper userMapper;
	
	@Override
	public EasyUIGrid findByParams(EasyUIPage easyUIPage, Map<String,String> params) {
		EasyUIGrid easyUIGrid = new EasyUIGrid();
		Page<Object> page = PageHelper.startPage(easyUIPage.getPage(), easyUIPage.getRows());
		List<UserQuery> users = userMapper.findByParams(params);
		easyUIGrid.setRows(users);
		easyUIGrid.setTotal(page.getTotal());
		return easyUIGrid;
	}

	@Override
	public Set<String> findRoles(String username) {
		List<Role> roles = userMapper.findRoles(username);
		Set<String> roleSet = new HashSet<String>();
		for(int i=0; i<roles.size(); i++){
			roleSet.add(roles.get(i).getRoleCode());
		}
		return roleSet;
	}
	
	@Override
	public Set<String> findPermissions(String username) {
		return new HashSet<String>();
	}
}
